package com.vichat.common.util;

import javapns.Push;
import javapns.communication.exceptions.CommunicationException;
import javapns.communication.exceptions.KeystoreException;
import javapns.devices.Device;
import javapns.devices.Devices;
import javapns.notification.PayloadPerDevice;
import javapns.notification.PushNotificationPayload;
import javapns.notification.transmission.PushQueue;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONException;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * Created by ranjx on 2017/3/1.
 * 利用javapns对IOS进行推送消息
 */
public class IosPushUtil {
    public static String keystore1 = null;
    public static String keystore2 = null;
    public static String password = null;
    public static String host = null;
    public static Boolean production = true;
    public static final int numberOfThreads = 8;
    static{
        Properties propertie = new Properties();
        InputStream inputStream;
        try {
            inputStream = IosPushUtil.class.getClassLoader().getResourceAsStream("push.properties");
            propertie.load(inputStream);
            keystore1 = propertie.getProperty("certificatePath1");
            keystore2 = propertie.getProperty("certificatePath2");
            password = propertie.getProperty("certificatePassword");
            host = propertie.getProperty("host");
            production = Boolean.valueOf(propertie.getProperty("production"));
            inputStream.close();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 推送一个简单消息
     * @param msg 消息
     * @param devices 设备
     * @throws CommunicationException
     * @throws KeystoreException
     */
    public static void pushMsgNotification(String msg,Object devices,String keystore) throws CommunicationException, KeystoreException{
        Push.alert(msg, keystore, password, production, devices);
    }
    /**
     * 推送一个标记
     * @param badge 标记
     * @param devices 设备
     * @throws CommunicationException
     * @throws KeystoreException
     */
    public static void pushBadgeNotification(int badge,Object devices,String keystore) throws CommunicationException, KeystoreException{
        Push.badge(badge, keystore, password, production, devices);
    }
    /**
     * 推送一个语音
     * @param sound 语音
     * @param devices 设备
     * @throws CommunicationException
     * @throws KeystoreException
     */
    public static void pushSoundNotification(String sound,Object devices,String keystore) throws CommunicationException, KeystoreException{
        Push.sound(sound, keystore, password, production, devices);
    }
    /**
     * 推送一个alert+badge+sound通知
     * @param message 消息
     * @param badge 标记
     * @param sound 声音
     * @param devices 设备
     * @throws CommunicationException
     * @throws KeystoreException
     */
    public static void pushCombinedNotification(String message,int badge,String sound,Object devices,String keystore) throws CommunicationException, KeystoreException{
        Push.combined(message, badge, sound, keystore, password, production, devices);
    }
    /**
     * 通知Apple的杂志内容
     * @param devices 设备
     * @throws CommunicationException
     * @throws KeystoreException
     */
    public static void contentAvailable(Object devices,String keystore) throws CommunicationException, KeystoreException{
        Push.contentAvailable(keystore, password, production, devices);
    }
    /**
     * 推送有用的调试信息
     * @param devices 设备
     * @throws CommunicationException
     * @throws KeystoreException
     */
    public static void test(Object devices,String keystore) throws CommunicationException, KeystoreException{
        Push.test(keystore, password, production, devices);
    }
    /**
     * 推送自定义负载
     * @param devices
     * @param msg
     * @param badge
     * @param sound
     * @param map
     * @throws JSONException
     * @throws CommunicationException
     * @throws KeystoreException
     */
    public static void pushPayload(List<Device> devices, String msg,Integer badge,String sound,Map<String,String> map,String keystore) throws JSONException, CommunicationException, KeystoreException{
        PushNotificationPayload payload = customPayload(msg, badge, sound, map);
        Push.payload(payload, keystore, password, production, devices);
    }
    /**
     * 用内置线程推送负载信息
     * @param devices
     * @param msg
     * @param badge
     * @param sound
     * @param map
     * @throws Exception
     */
    public static void pushPayLoadByThread(List<Device> devices, String msg,Integer badge,String sound,Map<String,String> map,String keystore) throws Exception{
        PushNotificationPayload payload = customPayload(msg, badge, sound, map);
        Push.payload(payload, keystore, password, production, numberOfThreads, devices);
    }
    /**
     * 推送配对信息
     * @param devices
     * @param msg
     * @param badge
     * @param sound
     * @param map
     * @throws JSONException
     * @throws CommunicationException
     * @throws KeystoreException
     */
    public static void pushPayloadDevicePairs(List<Device> devices,String msg,Integer badge,String sound,Map<String,String> map,String keystore) throws JSONException, CommunicationException, KeystoreException{
        List<PayloadPerDevice> payloadDevicePairs = new ArrayList<PayloadPerDevice>();
        PayloadPerDevice perDevice = null;
        for (int i = 0; i <devices.size(); i++) {
            perDevice = new PayloadPerDevice(customPayload(msg+"--->"+i, badge, sound, map), devices.get(i));
            payloadDevicePairs.add(perDevice);
        }
        Push.payloads(keystore, password, production, payloadDevicePairs);
    }
    /**
     * 用线程推配对信息
     * @param devices
     * @param msg
     * @param badge
     * @param sound
     * @param map
     * @throws Exception
     */
    public static void pushPayloadDevicePairsByThread(List<Device> devices,String msg,Integer badge,String sound,Map<String,String> map,String keystore) throws Exception{
        List<PayloadPerDevice> payloadDevicePairs = new ArrayList<PayloadPerDevice>();
        PayloadPerDevice perDevice = null;
        for (int i = 0; i <devices.size(); i++) {
            perDevice = new PayloadPerDevice(customPayload(msg+"--->"+i, badge, sound, map), devices.get(i));
            payloadDevicePairs.add(perDevice);
        }
        Push.payloads(keystore, password, production,numberOfThreads, payloadDevicePairs);
    }
    /**
     * 队列多线程推送
     * @param devices
     * @param msg
     * @param badge
     * @param sound
     * @param map
     * @throws KeystoreException
     * @throws JSONException
     */
    public static void queue(List<Device> devices,String msg,Integer badge,String sound,Map<String,String> map,String keystore) throws KeystoreException, JSONException{
        PushQueue queue = Push.queue(keystore, password, production, numberOfThreads);
        queue.start();
        PayloadPerDevice perDevice = null;
        for (int i = 0; i <devices.size(); i++) {
            perDevice = new PayloadPerDevice(customPayload(msg, badge, sound, map), devices.get(i));
            queue.add(perDevice);
        }
    }
    /**
     * 自定义负载
     * @param msg
     * @param badge
     * @param sound
     * @param map 自定义字典
     * @return
     * @throws JSONException
     */
    private static PushNotificationPayload customPayload(String msg,Integer badge,String sound,Map<String,String> map) throws JSONException{
        PushNotificationPayload payload = PushNotificationPayload.complex();
        if(StringUtils.isNotEmpty(msg)){
            payload.addAlert(msg);
        }
        if(badge != null){
            payload.addBadge(badge);
        }
        payload.addSound(StringUtils.defaultIfEmpty(sound, "default"));
        if(map!=null && !map.isEmpty()){
            Object[] keys = map.keySet().toArray();
            Object[] vals = map.values().toArray();
            if(keys!= null && vals != null && keys.length == vals.length){
                for (int i = 0; i < map.size(); i++) {
                    payload.addCustomDictionary(String.valueOf(keys[i]),String.valueOf(vals[i]));
                }
            }
        }
        return payload;
    }

    public static void main(String[] args) throws Exception {
        //pushMsgNotification("hello!!!2", true, "iostoken");
        //pushBadgeNotification(1,  "iostoken");
        /*String[] devs= new String[10000];
        for (int i = 0; i < devs.length; i++) {
            devs[i] = "iostoken";
        }*/
        //List<Device> devices=Devices.asDevices(devs);
        //System.out.println(devices.size());
        //pushPayLoadByThread(devices, "Hello 2222222", 1, null, null);
        //pushPayloadDevicePairs(devices, "Hello 111111111", 1, null, null);
        //pushPayloadDevicePairs(devices, "Hello +++", 1, null, null);
        //queue(devices,"Hello 2222222", 1, null, null);
        //普通：5CD48EE46A706A2971ED2BCDF3C48B43219DEDE64AD3C28C812501CA23264CA0
        //voip：F4C4D18CB10141B6F0EC88DEF219CCB404F86FE7EE0A8023EAF7A1D9069042AF
        HashMap map = new HashMap();
        map.put("uid","300000484");
        map.put("uname","李文博");
        map.put("type","text");
        map.put("rid","0");
        map.put("msg","呵呵");
        String device = "8e3314fd947a4abe15c8b9a83763cbe182a97a35f704c685ed451a7d2bc0e1de";
//        String msg = JSON.toJSONString(map);
//        pushMsgNotification(msg,device,keystore1);

        List<Device> devices= Devices.asDevices(device);
        pushPayLoadByThread(devices, "呵呵", 1, null, map, keystore1);
    }
}
